// pages/my/vip/vip_invitation/vip_invitation_share/index.js
const ControllerMy = require('../../../../controllers/ControllerMy').controller;
const Controller = require('../../../../controllers/Controller.js').controller;

const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    num: '',
    userMsg: {},
    invitationId: '',
    invitationAccountId: '',
    num: '',
    invitationInfo: {},
    shopDetail: {},
    imageList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      invitationId: options.invitationId,
      invitationAccountId: options.invitationAccountId,
      num: options.num,
    })
  },

  onShow: function () {
    if (wx.getStorageSync('usertoken')) {
      this.getUserMsg()
      this.getInvitationDetail()
    } else {
      //无usertoken，等待login登录回调
      app.usertokenReadyCallback = res => {
        if (res.data.sessionId) {
          this.getUserMsg()
          this.getInvitationDetail()
        }
      }
    }
  },
  getUserMsg() {
    Controller.getAccountDetail().then(res => {
      this.setData({
        userMsg: res.data,
      })
    })
  },
  // 获取邀请详情
  getInvitationDetail() {
    let value = {
      invitationId: this.data.invitationId,
      invitationAccountId: this.data.invitationAccountId,
      sessionId: wx.getStorageSync('usertoken'),
    }
    ControllerMy.invitationDetail(value).then(res => {
      let shop = res.data.shop
      app.globalData.shopId = shop.id
      wx.setStorageSync('shopId', shop.id)

      res.data.agreeTime = res.data.agreeTime.slice(0, 16)
      let imageList = res.data.image
      imageList = imageList ? imageList.split(',') : []
      this.setData({
        invitationInfo: res.data,
        shopDetail: res.data.shop,
        imageList
      })
    })
  },

  handleClickReceive() {
    const invitationId = this.data.invitationId
    const accountId = wx.getStorageSync('accountId')
    const sendAccountId = this.data.invitationInfo.sendInvitationAccount.accountId
    const sendNum = this.data.invitationInfo.sendInvitationAccount.num
    const num = this.data.num
    if (accountId == sendAccountId) {
      wx.showToast({
        title: '不可领取自己发送的邀请函',
        icon: 'none',
        duration: 1500
      });
      setTimeout(() => {
        wx.navigateTo({
          url: '/pages/my/vip/vip_invitation/vip_invitation_detail/index?invitationId=' + invitationId,
        })
      }, 1500);
      return
    }else if(num > sendNum){
      wx.showToast({
        title: '数量不足，领取失败',
        icon: 'none',
        duration: 2000
      });
      return
    }

    const that = this
    wx.requestSubscribeMessage({
      tmplIds: ["VahXCIaUZHNHyIIP6LYakcmroqlbOe8Q-qlMLJ5-Pps"],
      complete() {
        that.receive()
      }
    })
  },
  // 领取邀请函
  receive() {
    const invitationAccountId = this.data.invitationAccountId
    const invitationId = this.data.invitationId
    const num = this.data.num

    
    let value = {
      num,
      invitationAccountId,
      sessionId: wx.getStorageSync('usertoken'),
    }


    ControllerMy.invitationReceive(value).then(res => {
      wx.showToast({
        title: '领取成功',
      })
      setTimeout(() => {
        wx.navigateTo({
          url: '/pages/my/vip/vip_invitation/vip_invitation_detail/index?invitationId=' + invitationId,
        })
      }, 1000);
    })
    
  },
  //打开地址
  openAddress(e) {
    var lng = this.data.shopDetail.lng || '';
    var lat = this.data.shopDetail.lat || '';
    wx.openLocation({
      //当前经纬度
      latitude: Number(lat),
      longitude: Number(lng),
      //缩放级别默认28
      scale: 28,
    })
  },
  //打电话
  calling() {
    let phone = this.data.shopDetail.mobile
    wx.makePhoneCall({
      phoneNumber: phone,
      success: function () {
        console.log("拨打电话成功！")
      },
      fail: function () {
        console.log("拨打电话失败！")
      }
    })
  },
})